﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using System.Data.SqlClient;

namespace DiChanAPI.Controllers
{
    public class AuthorityController : ApiController
    {
         //GET api/authority
        [HttpGet]
        
        public IEnumerable<Authority> GetAuthority()
        {
            return new AuthorityBLL().GetListAuthority().Where(n => n.ParentId == 0);
        }
        [HttpGet]
        [ActionName("GetAuthorityChild")]
        public IEnumerable<Authority> GetAuthorityChild(int id)
        {
            return new AuthorityBLL().GetListAuthority().Where(n => n.ParentId == id);
        }
        // GET api/authority/5
        public string Get(int id)
        {
            return "value";
        }
        [HttpPost]
        [ActionName("PostAuthorityAndRoles")]
        // POST api/authority
        public bool PostAuthorityAndRoles(dynamic obj)
        {
            string  strName = Convert.ToString(obj.str);
            string[] arr = strName.Split(',');
           
            AuthorityAndRoles  list=Newtonsoft.Json.JsonConvert.DeserializeObject<AuthorityAndRoles>(Convert.ToString(obj.List));
             new AuthorityAndRolesBLL().DeleteAuthorityAndRoles(list.Roles_Id);
             int i = 0;
             foreach (var item in arr)
             {

                 SqlParameter[] param ={
                                      new SqlParameter("@Authority_Id",item),
                                      new SqlParameter("@Roles_Id",list.Roles_Id),
                                  };
                  new AuthorityAndRolesBLL().AddAuthorityAndRoles(param);
                  i++;
             }
            if(i==arr.Count())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // PUT api/authority/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/authority/5
        public void Delete(int id)
        {
        }
    }
}
